package com.buba.controller;

import com.buba.pojo.Message;
import com.buba.pojo.Role;
import com.buba.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin(allowCredentials = "true"
        , originPatterns = "*")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/role")
    public List<Role> selA(){
        System.out.println(roleService.selA());
        return roleService.selA();
    }

    @RequestMapping("/sel")
    public List<Integer> sel(@RequestParam("roleId") Integer roleId){
        System.out.println( roleService.sel(roleId));
        return roleService.sel(roleId);
    }

    @RequestMapping("/addRole")
    public Message addRole(@RequestParam("roleName")
                           String roleName){
        int count = roleService.addRole(roleName);
        if (count==0)
            return new Message(0,"创建失败");
        return new Message(1,"创建成功");
    }

    @RequestMapping("/del")
    public Message del(@RequestParam("roleId")
                       String roleId){
        int count = roleService.del(roleId);
        if (count==0)
            return new Message(0,"删除失败");
        return new Message(1,"删除成功");
    }


    @RequestMapping("/addCun")
    public Message cun(@RequestParam("roleId") Integer roleId,@RequestParam("nodeId") String nodeId){

        Map<String,Object> map = new HashMap<>();
        map.put("roleId",roleId);
        map.put("nodeId",nodeId.split(","));
        System.out.println(map);
        int s = roleService.delete(String.valueOf(roleId));
        int count = roleService.cun(map);
        if (count==0)
            return new Message(0,"保存失败");
        return new Message(1,"保存成功");
    }




}
